package com.easyjava.builder.strategy.impl;

import com.easyjava.bean.TableInfo;
import com.easyjava.builder.BuildMapperXml;
import com.easyjava.builder.strategy.inf.MapperXmlStrategy;
import java.io.BufferedWriter;
import java.io.IOException;

/**
 * 查询列表
 */
public class XmlFieldSelectListImpl implements MapperXmlStrategy {

    @Override
    public void generateMapperXml(TableInfo tableInfo, BufferedWriter bw) throws IOException {
        bw.write("\t<!--查询列表-->\n");
        bw.write("\t<select id=\"selectList\" resultMap=\"" + BuildMapperXml.BASE_RESULT_MAP + "\">\n");
        bw.write("\t\tSELECT " + BuildMapperXml.includeBaseColumnList() + "\n");
        bw.write("\t\tFROM " + tableInfo.getTableName() + "\n");
        bw.write("\t\t" + BuildMapperXml.includeQueryCondition() + "\n");

        String orderBy = BuildMapperXml.SELECT_PREFIX + "orderBy";
        bw.write("\t\t<if test=\"" + orderBy + " != null and " + orderBy + " != ''\">\n");
        bw.write("\t\t\torder by ${" + orderBy + "}\n");
        bw.write("\t\t</if>\n");

        String simplePage = BuildMapperXml.SELECT_PREFIX + "simplePage";
        String start = simplePage + ".start";
        String end = simplePage + ".end";
        bw.write("\t\t<if test=\"" + simplePage + " != null\">\n");
        bw.write("\t\t\tlimit #{" + start + "}, #{" + end + "}\n");
        bw.write("\t\t</if>\n");
        bw.write("\t</select>\n\n");
    }
}
